from datetime import datetime
from flask_login import UserMixin
from sqlalchemy import DateTime, Numeric
from werkzeug.security import generate_password_hash, check_password_hash
from applications.exts import db


# # 商品表
class Goods(db.Model, UserMixin):
    __tablename__ = 'wx_goods'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='商品表id')
    price = db.Column(db.Float, nullable=False, default=0.00, comment='商品价格')
    gname = db.Column(db.String(35), nullable=False, comment='商品名称')
    gphoto = db.Column(db.String(120), nullable=False, comment='商品图片')
    gnum = db.Column(db.String(9), nullable=False, comment='商品数量')
    isdel = db.Column(db.Integer, nullable=False, default=0, comment='是否删除')
    is_on_sale = db.Column(db.Integer, nullable=False, default=1, comment='商品是否上架')
    unit = db.Column(db.String(31), default='杯', comment='商品单位，例如件、盒')
    content = db.Column(db.TEXT, comment='商品详细介绍')
    isnew = db.Column(db.Integer, default=1, comment='是否是新品')
    iscommon = db.Column(db.Integer, default=1, comment='是否是热卖')
    add_time = db.Column(db.DateTime, default=datetime.now(), comment='商品添加时间')
    update_time = db.Column(db.DateTime, comment='更新时间')
    sales = db.Column(db.Integer, nullable=False, default=0, comment='销量')
    # 设置连接
    # orders = db.relationship('Orders', backref='goods_order')
    # 设置与
    classifyid = db.Column(db.Integer, db.ForeignKey('wx_classify.classifyid'), nullable=False, comment='设置外建')

    def to_dict(self):
        res = {}
        for col in self.__table__.columns:
            if isinstance(col.type, DateTime):  # 判断类型是否为DateTime
                if not getattr(self, col.name):  # 判断实例中该字段是否有值
                    value = ""
                else:  # 进行格式转换
                    value = getattr(self, col.name).strftime("%Y-%m-%d %H:%M:%S")
            elif isinstance(col.type, Numeric):  # 判断类型是否为Numeric
                value = float(getattr(self, col.name))  # 进行格式转换
            else:  # 剩余的直接取值
                value = getattr(self, col.name)
            res[col.name] = value
        return res


# 分类表
class Classify(db.Model, UserMixin):
    __tablename__ = 'wx_classify'
    classifyid = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='商品分类表id')
    gname = db.Column(db.String(30), nullable=False, unique=True, comment='分类名称')

    goods = db.relationship('Goods', backref='classify')

    def __str__(self):
        return self.gname

    def to_dict(self):
        res = {}
        for col in self.__table__.columns:
            if isinstance(col.type, DateTime):  # 判断类型是否为DateTime
                if not getattr(self, col.name):  # 判断实例中该字段是否有值
                    value = ""
                else:  # 进行格式转换
                    value = getattr(self, col.name).strftime("%Y-%m-%d %H:%M:%S")
            elif isinstance(col.type, Numeric):  # 判断类型是否为Numeric
                value = float(getattr(self, col.name))  # 进行格式转换
            else:  # 剩余的直接取值
                value = getattr(self, col.name)
            res[col.name] = value
        return res
